<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%@include file="/esayuiStyle.jsp" %>
<script type="text/javascript">
	$(function(){
		$("#sectionTable").datagrid({
			pagination:true,
			idField:"id"
		})
	})
	//添加科室
	function add_section(){
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "添加科室",
			iconCls : "icon-add",
			width:400,
			height:300,
			modal:true,
			href : "section/form",
			onClose:function(){$(this).dialog("destroy"); },
			buttons:[{
				iconCls:"icon-ok",
				text:"确定",
				handler:function(){
					$("#sectionForm").form("submit",{
						url : "section/insert",
						success : function(data){
							d.dialog("close");
							$("#sectionTable").datagrid("reload");
						}
					});
				}
			},{
				iconCls:"icon-cancel",
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		});
	}
	//修改科室
	function edit_section(){
		var row = $("#sectionTable").datagrid("getSelected");
		if(row == null){
			return;
		}

		//如果选中了多个，只保留row这个
		$("#sectionTable").datagrid("clearSelections");
		$("#sectionTable").datagrid("selectRecord",row.id);
		
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "修改科室",
			iconCls : "icon-edit",
			width:400,
			height:300,
			modal:true,
			href : "section/form",
			onClose:function(){$(this).dialog("destroy"); },
			onLoad:function(){
				//发送异步请求，查询数据
				$.post("section/sid",{id:row.id},function(data){
					$("#sectionForm").form("load",data);
				});
			},
			buttons:[{
				iconCls:"icon-ok",
				text:"确定",
				handler:function(){
					$("#sectionForm").form("submit",{
						url : "section/edit",
						success : function(data){
							d.dialog("close");
							$("#sectionTable").datagrid("reload");
						}
					});
				}
			},{
				iconCls:"icon-cancel",
				text:"取消",
				handler:function(){
					d.dialog("close");
				}
			}]
		})
	}
	function delete_section(){
		var selRows = $("#sectionTable").datagrid("getSelections");
		if(selRows.length == 0){
			$.messager.alert("提示","请选择要删除的数据行！","warning");
			return;
		}
		$.messager.confirm("提示","确定要删除选中的数据吗？",function(r){
			if(r){
				//3. 发送异步请求，带选中行的编号
				//拼接删除条件
				var postData = "";
				$.each(selRows,function(i){
					postData += "ids="+this.id;
					if(i < selRows.length - 1){
						postData += "&";
					}
				});
				$.post("section/delete",postData,function(data){
					if(data.result == true){
						//4. 删除成功后，刷新表格 reload
						$("#sectionTable").datagrid("reload");
					}
				});
			}
		});
	}
</script>
<body>
<div id="tb">
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="add_section();" data-options="iconCls:'icon-add',plain:true">添加科室</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="edit_section();" data-options="iconCls:'icon-edit',plain:true">修改科室</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="delete_section();" data-options="iconCls:'icon-remove',plain:true">删除科室</a>
</div>
<table id="sectionTable"  title="Section"
        data-options="url:'section/list',method:'post',fitColumns:true,striped:true,rownumbers:true ">
    <thead>
        <tr>
        	<th data-options="field:'tyu',checkbox:true"></th>
        	<th data-options="field:'id',width:30,sortable:true">科室编号</th>
            <th data-options="field:'name',width:100,sortable:true">科室名称</th>
        </tr>
    </thead>
</table>
</body>

</html>